package demo.practice.leetcode_msjd;

public class Msjd0107_2 {

    public static void main(String[] args) {
        Msjd0107_2 msjd0107_2= new Msjd0107_2();
        msjd0107_2.rotate( new int[][]{{ 5, 1, 9,11},{ 2, 4, 8,10},{13, 3, 6, 7},{15,14,12,16}});
    }

    public void rotate(int[][] matrix) {
        int xmin = 0, ymin = 0, xmax = matrix.length - 1, ymax = matrix.length - 1;
        while (xmin < xmax) {
            rotate(matrix, xmin++, ymin++, xmax--, ymax--);
        }

    }

    private void rotate(int[][] matrix, int xmin, int ymin, int xmax, int ymax) {
        int len = xmax - xmin;
        for (int i = 0; i < len; i++) {
            int tmp= matrix[xmin][ymin+i];
            matrix[xmin][ymin+i]= matrix[ymax-i][xmin];
            matrix[ymax-i][xmin]=matrix[xmax][ymax-i];
            matrix[xmax][ymax-i]=matrix[xmin+i][ymax];
            matrix[xmin+i][ymax]=tmp;
        }
    }
}
